<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="records">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        label="名称"
        width="width"
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        label="描述"
        width="width"
      ></el-table-column>
      <el-table-column label="默认图片" width="140" align="center">
        <template slot-scope="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量（KG）"
        width="120"
      ></el-table-column>
      <el-table-column prop="price" label="价格（元）" width="120">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="info"
            icon="el-icon-bottom"
            size="mini"
            v-if="row.isSale == 0"
            @click="skuOnSale(row)"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-top"
            size="mini"
            v-else
            @click="skuCancelSale(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(row)"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[5, 10, 15]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>
    <!-- sku详情展示 -->
    <el-drawer :visible.sync="isShowDrawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuDetailInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuDetailInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuDetailInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="(tag, index) in skuDetailInfo.skuAttrValueList"
            :key="tag.id"
            style="margin: 0 4px"
            >{{ tag.attrId }}--{{ tag.skuId }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="600px">
            <el-carousel-item
              v-for="(image, index) in skuDetailInfo.skuImageList"
              :key="image.id"
            >
              <img :src="image.imgUrl" style="width: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1, //当前第几页
      limit: 5, //每页显示多少条数据
      records: [], //存储Sku列表的数据
      total: 0, //分页器一共有多少数据需要展示
      skuDetailInfo: {}, //存储sku详情信息
      isShowDrawer: false, //用于控制展示抽屉（sku详情）
    };
  },
  mounted() {
    //调用函数 挂载时获取数据
    this.getSkuList();
  },
  methods: {
    //获取skuList的方法
    async getSkuList() {
      const { page, limit } = this;
      let result = await this.$API.sku.reqSkuList(page, limit);
      if (result.code == 200) {
        //存储数据
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //分页器切换页数
    handleCurrentChange(page) {
      this.page = page;
      //获取列表
      this.getSkuList();
    },
    //数据展示数量切换
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //商品处于下架状态 点击切换为商品上架
    async skuOnSale(row) {
      try {
        await this.$API.sku.reqSkuOnSale(row.id);
        //成功  将其isSale属性置为1
        row.isSale = 1;
        this.$message({ type: "success", message: `${row.skuName}上架成功` });
      } catch (error) {
        this.$message({ type: "error", message: `${row.skuName}上架失败` });
      }
    },
    //商品处于上架状态 点击切换为商品下架
    async skuCancelSale(row) {
      try {
        await this.$API.sku.reqSkuCancelSale(row.id);
        //成功  将其isSale属性置为0
        row.isSale = 0;
        this.$message({ type: "warning", message: `${row.skuName}下架成功` });
      } catch (error) {
        this.$message({ type: "error", message: `${row.skuName}下架失败` });
      }
    },
    //编辑按钮
    edit(row) {
      this.$message.warning("正在开发中");
    },
    //获取sku详情的方法
    async getSkuInfo(row) {
      let result = await this.$API.sku.reqSkuDetailById(row.id);
      if (result.code == 200) {
        this.skuDetailInfo = result.data;
      }
      //打开抽屉  显示sku详情数据
      this.isShowDrawer = true;
    },
  },
};
</script>

<style scoped>
.el-row > .el-col-5 {
  font-size: 24px;
  text-align: right;
  font-weight: bold;
}
.el-row > .el-col-16 {
  font-size: 22px;
}
.el-col {
  margin: 10px;
}

>>>.el-carousel__button {
  height: 10px;
  width: 10px;
  border-radius: 50%;
  background: red;
}
</style>

<style >
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

</style>